<template>
  <el-dialog :visible="isShow" :title="{ add:'新增角色', edit:'修改角色'}[mode]" @close="closE">
    <el-form ref="item" label-width="100px" :model="form" :rules="rules">
      <el-form-item prop="name" label="角色名称">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item prop="description" label="角色描述">
        <el-input v-model="form.description" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align:center">
        <el-button @click="closE">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
// 导入新增请求信息
import { sysRolePost, sysRolePut } from '@/api/setting.js'
export default {
  data() {
    return {
      mode: 'add',
      isShow: false,
      form: {
        name: '111', // string必须角色名称
        description: '1111'	// string必须角色描述
      },
      rules: {
        name: [{ message: '必填', trigger: 'change' }],
        description: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    closE() {
    //   this.$emit('input', false)
      this.isShow = false
      this.$refs.item.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // submit() {
    //   console.log(11111)
    //   this.$refs.item.validate(async(result) => {
    //     if (result) {
    //       const res = await sysRolePost(this.form)
    //       console.log(res, 888888)
    //       this.$message.success('新增成功')
    //       this.closE()
    //       //   this.isShow = false
    //       // 新增成功刷新列表
    //       this.$emit('getDate')
    //     }
    //   })
    // }
    submit() {
      console.log(11111)
      this.$refs.item.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await sysRolePost(this.form)
            this.$message.success('新增成功')
          } else {
            await sysRolePut(this.form)
            this.$message.success('编辑成功')
          }

          this.closE()
          //   this.isShow = false
          // 新增成功刷新列表
          this.$emit('getDate')
        }
      })
    }
  }
}
</script>

<style>

</style>
